import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import { dirname, resolve } from 'node:path'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import vueDevTools from 'vite-plugin-vue-devtools'
import legacy from '@vitejs/plugin-legacy'

// https://vite.dev/config/
export default defineConfig({
  base: './',

  plugins: [
    vue(),
    vueJsx(),
    vueDevTools(),
    legacy({
      targets: ['defaults', 'not IE 11'],
      // 不生成 modern 版本的 chunk but crossorigin attr not remove open index.html can't view
      renderModernChunks: false,
    }),
    function viteCrossoriginUseCredentials() {
      return {
        name: 'vite:crossorigin-use-credentials',
        transformIndexHtml(html) {
          return html.replace(/crossorigin/g, '');
        },
        enforce: 'post',
        apply: 'build',
      }
    }(),
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    },
  },

  build: {
    polyfillModulePreload: false,
    modulePreload: false,
    rollupOptions: {
      input: {
        main: resolve(__dirname, 'index.html'),
        nested: resolve(__dirname, 'test.html'),
      },
    },
  },
})
